package com.funong.matrix.biz.base.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.funong.matrix.biz.base.entity.SysRoleMenuEntity;
import com.funong.matrix.biz.base.mapper.SysRoleMenuMapper;
import com.funong.matrix.core.base.Dao;
import com.funong.matrix.core.base.FunongBaseDao;
import com.funong.matrix.core.enums.IsDeletedEnum;
import com.funong.matrix.core.utils.DateTimeUtil;
import org.aspectj.lang.annotation.Aspect;

import java.util.List;

/**
 * @author : NaNa
 * @since : 2023-05-19 16:41
 **/
@Aspect
@Dao
public class SysRoleMenuDao extends FunongBaseDao<SysRoleMenuMapper, SysRoleMenuEntity> {
    public List<SysRoleMenuEntity> getMenuListByRoleCodeList(List<String> roleCodeList) {
        return this.list(new LambdaQueryWrapper<SysRoleMenuEntity>()
                .in(!CollectionUtils.isEmpty(roleCodeList), SysRoleMenuEntity::getRoleCode, roleCodeList)
                .eq(SysRoleMenuEntity::getIsDeleted, IsDeletedEnum.NOT_DELETED.getValue()));
    }

    /**
     * 根 据角色编码删除
     *
     * @param roleCode
     */
    public void deleteRoleMenu(String roleCode) {
        new LambdaUpdateChainWrapper<>(this.baseMapper)
                .eq(SysRoleMenuEntity::getRoleCode, roleCode).remove();
    }

    /**
     * 根据角色编码保存
     *
     * @param roleCode
     * @param menuCodeList
     */
    public void saveRoleMenu(String roleCode, List<String> menuCodeList) {
        if (CollectionUtils.isNotEmpty(menuCodeList)) {
            for (String code : menuCodeList) {
                SysRoleMenuEntity entity = new SysRoleMenuEntity().setRoleCode(roleCode).setMenuCode(code);
                entity.setCreatedAt(DateTimeUtil.now());
                entity.setUpdatedAt(DateTimeUtil.now());
                this.save(entity);
            }
        }
    }
}
